home *** CD-ROM | disk | FTP | other *** search
/ Action Arcade 1997 / Action Arcade 1997.iso / ps / fmxutils.pas < prev    next >
Pascal/Delphi Source File  |  1996-06-10  |  4KB  |  125 lines

  1. unit FmxUtils;
  2.  
  3. interface
  4.  
  5. uses SysUtils, Windows, Classes, Consts;
  6.  
  7. type
  8.   EInvalidDest = class(EStreamError);
  9.   EFCantMove = class(EStreamError);
  10.  
  11. procedure CopyFile(const FileName, DestName: string);
  12. procedure MoveFile(const FileName, DestName: string);
  13. function GetFileSize(const FileName: string): LongInt;
  14. function FileDateTime(const FileName: string): TDateTime;
  15. function HasAttr(const FileName: string; Attr: Word): Boolean;
  16. function ExecuteFile(const FileName, Params, DefaultDir: string;
  17.   ShowCmd: Integer): THandle;
  18.  
  19. implementation
  20.  
  21. uses Forms, ShellAPI;
  22.  
  23. const
  24.   SInvalidDest = 'Destination %s does not exist';
  25.   SFCantMove = 'Cannot move file %s';
  26.  
  27. procedure CopyFile(const FileName, DestName: TFileName);
  28. var
  29.   CopyBuffer: Pointer; { buffer for copying }
  30.   TimeStamp, BytesCopied: Longint;
  31.   Source, Dest: Integer; { handles }
  32.   Destination: TFileName; { holder for expanded destination name }
  33. const
  34.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  35. begin
  36.   Destination := ExpandFileName(DestName); { expand the destination path }
  37.   if HasAttr(Destination, faDirectory) then { if destination is a directory... }
  38.     Destination := Destination + '\' + ExtractFileName(FileName); { ...clone file name }
  39.   TimeStamp := FileAge(FileName); { get source's time stamp }
  40.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  41.   try
  42.     Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  43.     if Source < 0 then raise EFOpenError.Create(FmtLoadStr(SFOpenError, [FileName]));
  44.     try
  45.       Dest := FileCreate(Destination); { create output file; overwrite existing }
  46.       if Dest < 0 then raise EFCreateError.Create(FmtLoadStr(SFCreateError, [Destination]));
  47.       try
  48.         repeat
  49.           BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }
  50.           if BytesCopied > 0 then { if we read anything... }
  51.             FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  52.         until BytesCopied < ChunkSize; { until we run out of chunks }
  53.       finally
  54.         FileClose(Dest); { close the destination file }
  55.       end;
  56.     finally
  57.       FileClose(Source); { close the source file }
  58.     end;
  59.   finally
  60.     FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  61.   end;
  62. end;
  63.  
  64.  
  65. { MoveFile procedure }
  66. {
  67.   Moves the file passed in FileName to the directory specified in DestDir.
  68.   Tries to just rename the file.  If that fails, try to copy the file and
  69.   delete the original.
  70.  
  71.   Raises an exception if the source file is read-only, and therefore cannot
  72.   be deleted/moved.
  73. }
  74.  
  75. procedure MoveFile(const FileName, DestName: string);
  76. var
  77.   Destination: string;
  78. begin
  79.   Destination := ExpandFileName(DestName); { expand the destination path }
  80.   if not RenameFile(FileName, Destination) then { try just renaming }
  81.   begin
  82.     if HasAttr(FileName, faReadOnly) then  { if it's read-only... }
  83.       raise EFCantMove.Create(Format(SFCantMove, [FileName])); { we wouldn't be able to delete it }
  84.       CopyFile(FileName, Destination); { copy it over to destination...}
  85. //      DeleteFile(FileName); { ...and delete the original }
  86.   end;
  87. end;
  88.  
  89. { GetFileSize function }
  90. {
  91.   Returns the size of the named file without opening the file.  If the file
  92.   doesn't exist, returns -1.
  93. }
  94.  
  95. function GetFileSize(const FileName: string): LongInt;
  96. var
  97.   SearchRec: TSearchRec;
  98. begin
  99.   if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
  100.     Result := SearchRec.Size
  101.   else Result := -1;
  102. end;
  103.  
  104. function FileDateTime(const FileName: string): System.TDateTime;
  105. begin
  106.   Result := FileDateToDateTime(FileAge(FileName));
  107. end;
  108.  
  109. function HasAttr(const FileName: string; Attr: Word): Boolean;
  110. begin
  111.   Result := (FileGetAttr(FileName) and Attr) = Attr;
  112. end;
  113.  
  114. function ExecuteFile(const FileName, Params, DefaultDir: string;
  115.   ShowCmd: Integer): THandle;
  116. var
  117.   zFileName, zParams, zDir: array[0..79] of Char;
  118. begin
  119.   Result := ShellExecute(Application.MainForm.Handle, nil,
  120.     StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
  121.     StrPCopy(zDir, DefaultDir), ShowCmd);
  122. end;
  123.  
  124. end.
  125.